<?php

class Public_BlogRssSuccessView extends BlogPublicBaseView
{
    /**
     * Form RSS xml and send it back to user.
     *
     * @param AgaviRequestDataHolder $rd
     * @return string
     */
	public function executeRss(AgaviRequestDataHolder $rd)
	{
	    /* @var $model Public_PostsModel */
	    $model = $this->getContext()->getModel('Posts', 'Public');
	    $ro = $this->getContext()->getRouting();
	    $blog_url = $ro->gen('root', array(), array('relative'=>FALSE));
	    $feed_url = $ro->gen('feeds.blog', array(), array('relative'=>FALSE));
	    
	    $atom = new AtomGenerator($blog_url, $blog_url, $feed_url, WereWordConfig::get('blog.name'), 
	        date_create(null), WereWordConfig::get('blog.owner_name'));
	    
	    $posts = $model->findBlogPosts(0, 5, TRUE);
	    foreach ($posts as $post) {
	        /* @var $post Post */
	        $title = MarkupProcessor::getPostTitleHtml($post);
	        
	        $route_elements = $model->getPostCoordinates($post);
	        $post_url = $ro->gen('post.view', $route_elements, array('relative'=>FALSE));
	        
	        $content = MarkupProcessor::getPostSummaryHtml($post);
	        
            $tags = array();
            foreach ($post->Tags as $tag) {
                $tags[] = $tag->tag;
            }
                
	        $atom->appendEntry($post_url, $post_url, $title, date_create($post->mod_time), 
	            date_create($post->post_time), $content, $tags);
	    }
	    
	    
	    $this->getResponse()->setContentType('application/atom+xml; charset=UTF-8');
	    return $atom->dump();
	}
}

?>
